<?php
// 显示所有错误
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// 引入DatabaseHelper类
require_once 'includes/DatabaseHelper.php';

// 测试数据库连接
try {
    // 获取数据库实例
    $db = Database::getInstance();
    
    // 尝试执行简单查询
    $tables = $db->fetchAll("SHOW TABLES");
    
    // 输出结果
    $success = true;
    $message = "数据库连接和查询成功！";
    $result = [
        'tables' => $tables,
        'database_class' => get_class($db),
        'connection_active' => ($db->getConnection() instanceof PDO)
    ];
} catch (Exception $e) {
    // 捕获异常
    $success = false;
    $message = "错误: " . $e->getMessage();
    $result = [
        'error_code' => $e->getCode(),
        'trace' => $e->getTraceAsString()
    ];
}
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>KidsMind - 数据库测试</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css">
</head>
<body>
    <div class="container mt-5">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card shadow">
                    <div class="card-header <?php echo $success ? 'bg-success' : 'bg-danger'; ?> text-white">
                        <h4 class="mb-0">KidsMind 数据库测试</h4>
                    </div>
                    <div class="card-body">
                        <div class="alert <?php echo $success ? 'alert-success' : 'alert-danger'; ?>">
                            <strong><?php echo $message; ?></strong>
                        </div>
                        
                        <?php if ($success): ?>
                            <h5>数据库信息:</h5>
                            <ul class="list-group mb-4">
                                <li class="list-group-item">使用类: <?php echo $result['database_class']; ?></li>
                                <li class="list-group-item">连接状态: <?php echo $result['connection_active'] ? '活跃' : '未连接'; ?></li>
                            </ul>
                            
                            <h5>数据库表:</h5>
                            <table class="table table-bordered table-striped">
                                <thead>
                                    <tr>
                                        <th>表名</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($result['tables'] as $table): ?>
                                        <tr>
                                            <td><?php echo htmlspecialchars(reset($table)); ?></td>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        <?php else: ?>
                            <h5>错误信息:</h5>
                            <pre class="bg-light p-3"><?php echo htmlspecialchars(print_r($result, true)); ?></pre>
                        <?php endif; ?>
                        
                        <div class="mt-4">
                            <a href="index.php" class="btn btn-primary">返回控制台</a>
                            <a href="fix_database_class.php" class="btn btn-secondary">重新运行修复工具</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html> 